package com.alog.yzd.bookmall.web.servlet.admin.category;

import com.alog.yzd.bookmall.domain.catagory.Catagory;
import com.alog.yzd.bookmall.service.book.BookService;
import com.alog.yzd.bookmall.service.catagory.CatagoryService;
import com.alog.yzd.commons.CommonUtils;
import com.alog.yzd.commons.Print;
import com.alog.yzd.commons.servlet.BaseServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Auther: Ye Zhengdong
 * @Date: 2019/1/10 16:19
 * @Description:
 */
public class AdminCategoryServlet extends BaseServlet {
    private CatagoryService catagoryService = new CatagoryService();
    private BookService bookService = new BookService();
    /**
     * 删除二级分类
     */
    public String deleteChild(HttpServletRequest req, HttpServletResponse resp){
        String cid = req.getParameter("cid");
        int bookCount = bookService.findbookCountByCatagory(cid);
        if(bookCount > 0){
            req.setAttribute("code", "error");
            req.setAttribute("msg", "该分类下还有图书, 不能删除");
            return "f:/adminjsps/admin/msg.jsp";
        } else {
            catagoryService.delete(cid);
            return findAll(req, resp);
        }
    }

    /**
     * 删除一级分类
     */
    public String deleteParent(HttpServletRequest req, HttpServletResponse resp){
        String pid = req.getParameter("pid");
        int count = catagoryService.findChildCountbyParent(pid);
        if(count > 0){
            req.setAttribute("code", "error");
            req.setAttribute("msg", "该分类下还有子分类, 不能删除");
            return "f:/adminjsps/admin/msg.jsp";
        } else {
            catagoryService.delete(pid);
            return findAll(req, resp);
        }
    }
    /**
     * 修改二级分析第二步
     */
    public String editChild(HttpServletRequest req, HttpServletResponse resp){
        Catagory catagory = CommonUtils.toBean(req.getParameterMap(), Catagory.class);
        String pid = req.getParameter("pid");
        Catagory parent = new Catagory();
        parent.setCid(pid);
        catagory.setParent(parent);
        catagoryService.edit(catagory);
        return findAll(req, resp);

    }

    /**
     * 修改二级分析第一步
     */
    public String editChildPre(HttpServletRequest req, HttpServletResponse resp){
        String cid = req.getParameter("cid");
        Catagory catagory = catagoryService.load(cid);
        req.setAttribute("child", catagory);
        req.setAttribute("parents", catagoryService.findParents());
        return "f:/adminjsps/admin/category/edit2.jsp";

    }

    /**
     * 修改一级分析第二步
     */
    public String editParent(HttpServletRequest req, HttpServletResponse resp){
        Catagory catagory = CommonUtils.toBean(req.getParameterMap(), Catagory.class);
        catagoryService.edit(catagory);
        return findAll(req, resp);

    }

    /**
     * 修改一级分析第一步
     */
    public String editParentPre(HttpServletRequest req, HttpServletResponse resp){
        String cid = req.getParameter("cid");
        Catagory catagory = catagoryService.load(cid);
        req.setAttribute("parent", catagory);
        return "f:/adminjsps/admin/category/edit.jsp";

    }

    /**
     * 添加二级分类
     */
    public String addChild(HttpServletRequest req, HttpServletResponse resp){
        Catagory catagory = CommonUtils.toBean(req.getParameterMap(), Catagory.class);
        catagory.setCid(CommonUtils.uuid());
        String pid = req.getParameter("pid");
        Catagory parent = new Catagory();
        parent.setCid(pid);
        catagory.setParent(parent);
        catagoryService.add(catagory);
        return findAll(req, resp);
    }

    /**
     * 添加二级分类准备工作
     */
    public String addChildPre(HttpServletRequest req, HttpServletResponse resp){
        String cid = req.getParameter("pid");
        List<Catagory> parents = catagoryService.findParents();
        req.setAttribute("pid", cid);
        req.setAttribute("parents", parents);
        Print.print("parents:" + parents.size());
        return "f:/adminjsps/admin/category/add2.jsp";
    }
    /**
     * 添加一级分类
     */
    public String addParent(HttpServletRequest req, HttpServletResponse resp){
        Catagory catagory = CommonUtils.toBean(req.getParameterMap(), Catagory.class);
        catagory.setCid(CommonUtils.uuid());
        catagoryService.add(catagory);
        return findAll(req, resp);
    }

    /**
     * 查询所有分类
     */
    public String findAll(HttpServletRequest req, HttpServletResponse resp){
        List<Catagory> catagories = catagoryService.findAll();
        req.setAttribute("parents", catagories);
        return "f:/adminjsps/admin/category/list.jsp";
    }
}
